package com.fingard.dsp.bank.directConfig;

import java.util.ArrayList;
import java.util.HashMap;

import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;

import com.fingard.FGBiz;

public class DataPushSet {
	public String id;
	public String pushType;
	
	public String readTimeoutSec;
	
	/**设置秒，返回秒*/
    public int getReadTimeoutSec(int pDefSec) {
        //秒
        int timeout = pDefSec;
        if(StringHelper.hasAnyChar(readTimeoutSec)){
        	try {
                timeout = Integer.parseInt(readTimeoutSec);
            } catch (Exception ex) {
            	FGBiz.limitMsg.exception("DataPushSet", ex, "取readTimeout异常");
            }
        }
        return timeout;
    }
    
    /**单位：毫秒*/
    public int getReadTimeout(int pDefMilSec) {
        //秒
        int timeout = pDefMilSec;
        if(StringHelper.hasAnyChar(readTimeoutSec)){
        	try {
                timeout = Integer.parseInt(readTimeoutSec) * 1000;
            } catch (Exception ex) {
            	FGBiz.limitMsg.exception("DataPushSet", ex, "取readTimeout异常");
            }
        }
        return timeout;
    }
    
	private String charset;
	
	public String getCharset(String pDef){
		if(StringHelper.hasAnyChar(charset)){
			return charset;
		}else{
			return pDef;
		}
	}
	/**推送服务地址*/
	public String pushServerUrl;
	
	/**接收对方回调的地址*/
	public String acceptCallbackUrl;
	
	private HashMap<String,String> paramList;
	public String getParam(String pKey, String pFor){
		if(paramList!=null && paramList.size()>0){
			String firstKey = pKey+"-"+pFor;
			String[] chkKeys = new String[]{firstKey.toLowerCase(),"","*"};
			for(String ehKey : chkKeys){
				if(paramList.containsKey(ehKey)){
					return paramList.get(ehKey);
				}
			}
		}
		return "";
	}
	
	public void fromXElement(XElement p_xe) throws Exception{
		id = p_xe.getSingleTextNotNull("id");
		pushType = p_xe.getSingleTextNotNull("pushType");
		pushServerUrl = p_xe.getSingleTextNotNull("pushServerUrl");
		acceptCallbackUrl = p_xe.getSingleTextNotNull("acceptCallbackUrl");
		charset = p_xe.getSingleTextNotNull("charset");
		readTimeoutSec = p_xe.getSingleTextNotNull("readTimeoutSec");
		paramList = new HashMap<String,String>();
		XElement params = p_xe.getSingleChild("paramList");
		if(params != null){
			ArrayList<XElement> pList = params.getAllChildList();
			if(pList!=null && pList.size()>0){
				for(XElement pmXe : pList){
					String pmKey = pmXe.name + "-" + pmXe.getAttrValue("for");
					paramList.put(pmKey.toLowerCase(), pmXe.innerText);
				}
			}
		}
	}
}
